package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.domian.channel.NewTaskExecutionDTO;
import org.jsbd.boss.domian.channel.NewTaskExecutionVO;
import org.jsbd.boss.enums.LoginTypeEnum;
import org.jsbd.boss.service.INewTaskExecutionService;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.EnumUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/newTask/execution")
public class NewTaskExecutionStatisticsController {

  private static final Logger logger = LoggerFactory.getLogger(NewTaskExecutionStatisticsController.class);

  @Resource
  private INewTaskExecutionService newTaskExecutionService;

   /**
    * 新增任务执行量统计页面(按登录类型)
    * @return 
    */
  	@RequestMapping(value = "/loginType/list")
  	public String loginTypeList() {
  		return "newtask/executionLoginTypeList";
  	}
  	
  	/**
     * 新增任务执行量统计数据(按登录类型)
     * @return 新增任务执行量统计数据
     */
	@RequestMapping(value = "/loginType/listJson", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> loginTypeListJson(DataGridModel page, NewTaskExecutionVO nteVO) {
		String startTime = nteVO.getStartTime();
		String endTime = nteVO.getEndTime();
		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
			nteVO.setStartTime(DateUtil.getDayBeforeDate(DateUtil.DATE_PATTEN_YYMMDD, 30));
		}
		if (!StringUtils.isBlank(startTime)) {
			nteVO.setStartTime(startTime.replace("-", ""));
		}
		if (!StringUtils.isBlank(endTime)) {
			nteVO.setEndTime(endTime.replace("-", ""));
		}
		
		String mark = nteVO.getMark();
		if (StringUtils.isNotBlank(mark)) {
			String[] marks = mark.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : marks) {
				keywords.add(keyword);
			}
			nteVO.setKeywords(keywords);
		}
		
		return newTaskExecutionService.findNewTaskExecutionWithPage(page, nteVO);
	}
	
	/**
     * 新增任务执行量详情(按登录类型)
     * @return 
     */
	@RequestMapping(value = "/loginType/detailList")
	public String loginTypeDetailList(Model model, String mark, String day) {
		List<NewTaskExecutionDTO> nteDTOs  = newTaskExecutionService.findByMarkAndDay(mark, day);
		for (NewTaskExecutionDTO dto : nteDTOs) {
			dto.setLoginName(LoginTypeEnum.getMap().get(dto.getLoginType()).getName());
		}
		model.addAttribute("nteDTOs", nteDTOs);
		return "newtask/executionLoginTypeDetailList";
	}
	
	/**
    * 新增任务执行量统计页面(按登录类型查询包)
    * @return 
    */
  	@RequestMapping(value = "/mark/list")
  	public String markList(Model model) {
  		Map<Integer, LoginTypeEnum> map =  LoginTypeEnum.getMap();
  		Map<String, String> loginTypeMap = new HashMap<String, String>();
  		for (Map.Entry<Integer, LoginTypeEnum> entry : map.entrySet()) {
  			loginTypeMap.put(String.valueOf(entry.getKey()), entry.getValue().getName());
  		}
  		model.addAttribute("loginTypeMap", loginTypeMap);
  		return "newtask/executionMarkList";
  	}
  	
  	/**
     * 新增任务执行量统计数据(mark)
     * @return 新增任务执行量统计数据
     */
	@RequestMapping(value = "/mark/listJson", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> markListJson(DataGridModel page, NewTaskExecutionVO nteVO) {
		String startTime = nteVO.getStartTime();
		String endTime = nteVO.getEndTime();
		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
			nteVO.setStartTime(DateUtil.getDayBeforeDate(DateUtil.DATE_PATTEN_YYMMDD, 30));
		}
		if (!StringUtils.isBlank(startTime)) {
			nteVO.setStartTime(startTime.replace("-", ""));
		}
		if (!StringUtils.isBlank(endTime)) {
			nteVO.setEndTime(endTime.replace("-", ""));
		}
		
		Map<String, Object> resultMap = newTaskExecutionService.findNewTaskExecutionMarkWithPage(page, nteVO);
		List<NewTaskExecutionDTO> nteDtos = (List<NewTaskExecutionDTO>) resultMap.get("rows");
		for (NewTaskExecutionDTO nteDto : nteDtos) {
			Integer loginType = nteDto.getLoginType();
			LoginTypeEnum lte = LoginTypeEnum.getMap().get(loginType);
			if (null != lte) {
				nteDto.setLoginName(lte.getName());
			}
		}
		return resultMap;
	}
	
	/**
     * 新增任务执行量详情(mark)
     * @return 
     */
	@RequestMapping(value = "/loginType/detailMarkList")
	public String detailMarkList(Model model, String loginType, String day) {
		List<NewTaskExecutionDTO> nteDTOs  = newTaskExecutionService.findByLoginTypeAndDay(loginType, day);
		model.addAttribute("nteDTOs", nteDTOs);
		return "newtask/executionMarkDetailList";
	}
  	
    
}
